<template>
	<view class="page">
		<!-- <fu-custom bgColor="bg-white" :isBack="false">
			<block slot="content">登录</block>
		</fu-custom> -->
		<view class="solid-top">
			<!-- <view class="text-right text-df padding" @tap="cancle">跳过，看好物<text class="cuIcon-right"></text></view> -->
			<view class="header">
				<image src="/static/talent/login/logo.png"></image>
			</view>
			<!-- <view class="content">
				<view>申请获取以下权限</view>
				<text>获得你的公开信息(昵称，头像、地区等)</text>
			</view> -->
			<button class="bottom bg-white" type="" open-type="getUserInfo" withCredentials="true" lang="zh_CN"
				@getuserinfo="wxGetUserInfo">
				<!-- <image class="margin-right-xs" src="/static/talent/login/weixin.png" mode="aspectFit"></image> -->
				<text>登录</text>
			</button>
			<view class="text-center text-999 text-sm">
				<image @tap.stop="selectCheck" v-if="!check" class="check_img" src="/static/talent/home/payment_2.png" mode=""></image>
				<image @tap.stop="selectCheck" v-else class="check_img" src="/static/talent/home/payment.png" mode=""></image>
				<text>请阅读并同意</text>
				<text class="alink margin-lr-xs" @tap="navto(0)">《平台服务协议》</text>
				<text>和</text>
				<text class="alink margin-lr-xs" @tap="navto(1)">《隐私政策》</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				logo: global.SYSTEM_CONFIG.logo,
				user_id: '',
				invite_code: '',
				check: false
			}
		},
		onLoad(option) {
			console.log(option)
			this.user_id = option.id
			this.invite_code = option.invite_code
			console.log(this.invite_code)
		},
		methods: {
			// 取消登录
			cancle() {
				uni.navigateBack();
			},
			selectCheck() {
				this.check = !this.check;
			},
			// 第一授权获取用户信息===》按钮触发
			wxGetUserInfo() {
				if (!this.check) {
					this.$message.info('请阅读并同意平台服务协议和隐私政策');
					return;
				}
				uni.showLoading({
					title: '登陆中...'
				})
				var that = this

				uni.login({
					success: res => {
						const code = res.code;
						console.log(code)
						uni.getUserInfo({
							provider: 'weixin',
							success: (infoRes) => {
								console.log(infoRes)
								this.$api.post(global.apiUrls.postMiniparamLogin, {
									encryptedData: infoRes.encryptedData,
									iv: infoRes.iv,
									code: code,
								}).then(result => {
									result = result.data;
									console.log("后台返回登录信息", result)
									if (result.code == 1) {
										uni.hideLoading();
										if (result.data.result) {
											uni.setStorageSync('Wxinfo_tmp', result.data
												.result);
											setTimeout(function() {
												uni.reLaunch({
													url: '/pages/user/login/auth-phone?batId=' +
														that.user_id +
														'&invite_code=' +
														that.invite_code
												});
											}, 500);
										} else if (result.data.userinfo) {
											this.$message.info(result.msg);
											//存入本地
											global.userInfo = result.data.userinfo;
											uni.setStorageSync(
												'USER_INFO',
												result.data.userinfo
											);
											global.token = result.data.userinfo.user_token;
											// global.token = '9bd19fe6-678c-13fc-3736-56c3d5d05b50';
											uni.$emit('appLoginSuccess', () => {

											})
											// this.initializeSocket()
											uni.setStorageSync(
												'USER_TOKEN',
												result.data.userinfo.user_token
											);
											setTimeout(function() {
												uni.switchTab({
													url: '/pages/tab/home/shopindex'
												});
											}, 500);
										}
									} else if (result.code == -999) {
										uni.hideLoading();
										this.$message.info(result.msg);
									} else {
										uni.hideLoading();
										this.$message.info('授权失败，请使用其他方式登录');
									}
								}).catch(err => {
									console.log('ERROR', err);
									uni.hideLoading();
									this.$message.info(err.msg);
								})
							},
							fail: err => {
								console.log('ERROR', err);
								this.$message.info('获取用户授权失败');
								uni.hideLoading();
							}
						});
					},
					fail: err => {
						console.log('ERROR', err);
						uni.hideLoading();
					}
				});

			},
			navto(id) {
				this.$urouter.navigateTo({
					url: '/pages/user/login/user-agreement',
					params: {
						id: id
					}
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.check_img {
		width: 30rpx;
		height: 30rpx;
		vertical-align: middle;
		margin-right: 16rpx;
		transform: translateY(-3rpx);
	}
	.page {
		min-height: 100vh;
		background: #fff !important;
		text-align: center;

	}

	.solid-top {
		padding-top: 270rpx;
	}

	.header {
		height: 172rpx;
		width: 236rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		margin: 0 auto 214rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.content {
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		width: 640rpx;
		height: 88rpx;
		line-height: 88rpx;
		background: #006747;
		opacity: 1;
		border-radius: 8rpx;
		color: #FFFFFF;
		font-size: 32rpx;


		image {
			width: 42rpx;
			height: 34rpx;
			vertical-align: middle;
			transform: translateY(-4rpx);

		}

		text {
			margin-left: 6rpx;
		}
	}

	.bottom::after {
		display: none;
	}

	.alink {
		color: #009B77;
		font-weight: 800;
	}

	.text-999 {
		width: 100%;
		text-align: center;
		position: fixed;
		bottom: 80rpx;
		// margin-left: 20rpx;
		color: #6B6F73;
		font-size: 24rpx;
	}
</style>